<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/article/ShowSearch.class.php
 *
 * 显示文章搜索结果
 *
 * PHP Version 5
 *
 * @package:   class.article
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: ShowSearch.class.php,v 1.1 2006-09-24 14:38:33 ghw Exp $
 * @date:      $Date: 2006-09-24 14:38:33 $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

//包含我们需要的工具软件包
include_once CLASS_PATH.'article/CategoryUtil.class.php';
include_once CLASS_PATH.'article/ArticleUtil.class.php';
include_once FUNCTION_PATH.'getCurrentDate.fun.php';
include_once FUNCTION_PATH.'getIp.fun.php';
include_once FUNCTION_PATH.'ConvertString.fun.php';




//定义处理的类

class ShowSearch extends BaseAction {

   /**
    * 数据库的连接
    */
   private $db;

   /**
    * 每一页显示的记录数
    */
   private $page_number = 25;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }




   /**
    * 显示搜索的结果
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      
      //首先取得查询条件
      $sqlq = $this->getParameterFromGET('wq');

      $where_sql = '';

      if ( !$sqlq ) {
         $q = $this->getParameterFromGET('q');
         $q = trim($q);
         
         $q_array = preg_split("/\s+/", $q);

         if ( count($q_array) > 1 ) {
            
            $where_sql = " where ";

            for ( $i=0; $i< count($q_array); $i++ ) {
               $where_sql .= " title like '%".$q_array[$i]."%' ";

               if ( $i < count($q_array) - 1 ) {
                  $where_sql .= " or ";
               }
            }

         } else if ( count($q_array) == 1 ) {
            if ( $q && strlen($q) > 0 ) {
               $where_sql = " where title like '%".$q."%' ";
            }
         } 

      } else {
         if ( strlen($sqlq) ) {
            $where_sql = base64_decode($sqlq);
         }
      }

      //编码后的SQL
      $encode_sql = base64_encode($where_sql);
   


      $smarty = $this->getSmarty();

      //分配编码后的值给URL替换。
      $smarty->assign('wq', $encode_sql);

      //显示头
      $categroy = CategoryUtil::getTopCategory($this->db);
      $smarty->assign('catagroy', $categroy);

      //为SEO设置网站的信息
      $smarty->assign('clone_title', '文章搜索结果/5anet.com' );
      $smarty->assign('header_meta_content', '文章搜索结果/5anet.com' );

      //搜索的结果
      $sql = 'select count(*) as num from article '.$where_sql;

      $res = $this->db->Execute($sql);
      $rows = $res->FetchRow();

      $total_number = $rows['num'];

         //求总公的页面
         $total_page = ceil($total_number / $this->page_number );
      
         //取得当前的页面
         $page = $this->getParameter('page');

         if ( !$page || $page < 0 ) {
            $page = 1;
         }

         if ( $page > $total_page && $total_page > 0 ) {
            $page = $total_page;
         }

         $begin_page = 1;
         $end_page = $total_page;

         if ( $page <= 10 && $total_page >=10  ) {
            $end_page = 10; 
         } else if ( $page > 10  ) {
            if ( $page % 10 == 0 ) {
               //向前翻
               $end_page = $page; 

               $begin_page = $end_page - 9;
   
            } else if ( $page % 10 == 1 ) {
   
               //向后翻
               //确定开始的页数
               $begin_page = $page; 
   
               if ( $begin_page > $total_page ) {
                  $begin_page = $page - 9;
               }
   
               if ( ( $begin_page + 9 ) > $total_page ) {
                  $end_page = $total_page;
               } else {
                  $end_page = $begin_page + 9;
               } 
   
            } else {
               $num = $page % 10;
               $pre_num = floor($page / 10 );
               $begin_page = $pre_num * 10 + 1;
               $end_page = $begin_page + 9;
            }
         }

         if ( $end_page > $total_page ) {
            $end_page = $total_page;
         }

         $nav_page_array = array();
         for( $i = $begin_page; $i<=$end_page; $i++ ) {
            array_push($nav_page_array, $i);
         }
         //帖子导航栏
         $smarty->assign('nav_page', $nav_page_array);
         //当前的页面
         $smarty->assign('now_page', $page);
         //共有的页面
         $smarty->assign('total_page', $total_page);


         //求出偏移
         $offset_page = ( $page - 1 ) * $this->page_number;



         $sql = 'select id, title, author, fromaddr, clicks, submitDate from article '.
            $where_sql.' order by id desc ';

         $res = $this->db->CacheSelectLimit(120, $sql, $this->page_number, $offset_page);

         $article_info = array();

         while ( $rows = $res->FetchRow() ) {
            $article_info[] = array (
               'id'=>$rows['id'],
               'title'=>$rows['title'],
               'stitle'=>utf8_substr($rows['title'], 0, 30),
               'author'=>$rows['author'],
               'fromaddr'=>$rows['fromaddr'],
               'clicks'=>$rows['clicks'],
               'submitDate'=>$rows['submitDate']
            );
         }

         $smarty->assign('search', $article_info);







      $smarty->display('articlesearchresult.tmpl');


   }


}

?>
